AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('shared.lua')

function ENT:Initialize()
	self.Entity:SetModel( "models//jaanus//wiretool//wiretool_range.mdl" )
	
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	
	self.Outputs = Wire_CreateOutputs(self.Entity, { "Health" , "Max Health" })
	self.Inputs = Wire_CreateInputs(self.Entity, { "Activate" })
	self.WireDebugName = self.PrintName
	
	self.Entity:GetPhysicsObject():Wake()
	self.Contraption = {}
	self.Active = false
	self.CHealth = 0
	self.CMaxHealth = 0
end

function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end
	
	local SpawnPos = tr.HitPos + tr.HitNormal * 10
	
	local ent = ents.Create( "utility_health_meter" )
		ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	
	return ent
	
end

function ENT:TriggerInput(k,v)
	if (k == "Activate") then
		self.Active = util.tobool(v)
		self.Entity:UpdateContraption()
	end
end

function ENT:UpdateContraption()
	self.Contraption = CDE_FindContraption( self.Entity )
	self.CMaxHealth = CDamage_findcontraptionmaxhealth(self.Contraption)
	Wire_TriggerOutput(self.Entity, "Max Health", self.CMaxHealth)
end

function ENT:Think()
	if self.Active then
		self.CHealth = CDamage_findcontraptionhealth(self.Contraption)
		Wire_TriggerOutput(self.Entity, "Health", self.CHealth)
	end
	self.Entity:NextThink(CurTime() + 0.5)
	return true
end

